﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;

namespace project_assess.assess_relate
{
    public partial class finish_frm : Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        public finish_frm(Mfrm frm )
        {
            InitializeComponent();
            mfrm = frm;
            m_db = frm.m_db; load_tree(); btnchk.Enabled = false;
        }


        //是否完成材料埋设费的核算
        public bool check_material_finish(string id)
        {
            string sql = "select [班组提成] from [assess_material] where [项目编号]='" + id + "' and [次数]=1";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            double i = (double)dt.Rows[0][0];
            if (i == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //是否完成控制成本的核算
        public bool check_predict_finish(string id)
        {
            string sql = "select [费用] from [assess_predict] where [项目编号]='" + id + "'" +
                " and [控制成本]='班组提成'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            double i = (double)dt.Rows[0][0];
            if (i == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //是否完成管理考核的核算
        public bool check_manage_finish(string id)
        {
            string sql = "select [系数] from [assess_manage] where [项目编号]='" + id + "'" +
                " and [内容]='方案核算'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            sql = "select [系数] from [assess_manage] where [项目编号]='" + id + "'" +
                " and [内容]='总结核算'";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            sql = "select [系数] from [assess_manage] where [项目编号]='" + id + "'" +
       " and [内容]='工程质量核算'";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            sql = "select [系数] from [assess_manage] where [项目编号]='" + id + "'" +
" and [内容]='资料归档核算'";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            sql = "select [系数] from [assess_manage] where [项目编号]='" + id + "'" +
" and [内容]='其他'";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            return true;
        }
        //是否完成工作量的核
        public bool check_work_finish(string id)
        {
            string sql = "select [终止比例] from [assess_work] where [项目编号]='" + id + "'" +
                " order by [次数]";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                return false;
            }
            double i = (double)dt.Rows[dt.Rows.Count - 1][0];
            if (i != 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        void load_tree()
        {
            string sql = "select [项目编号],[项目名称] from [projects] where [状态]='在建'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            tree.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = dt.Rows[i][0].ToString();
                n.Cells.Add(new Cell(dt.Rows[i][1].ToString()));
                tree.Nodes.Add(n);
            }
        }

        //选择工程后
        private void tree_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            Node n = e.Node;
            if (n == null)
            {
                btnchk.Enabled = false;
                return;
            }
            string id = n.Text;
            string tip = string.Format("材料埋设费:{0}   控制成本:{1}\r\n\r\n管理考核:{2}   工作量考核:{3}"
                , new object[]{check_material_finish(id)==true?"完成":"未完成",
                    check_predict_finish(id)==true?"完成":"未完成",
                    check_manage_finish(id)==true?"完成":"未完成",
                    check_work_finish(id)==true?"完成":"未完成"});
            labelX1.Text = tip;
            bool t1 = check_material_finish(id);
            bool t2 = check_predict_finish(id);
            bool t3 = check_manage_finish(id);
            bool t4 = check_work_finish(id);
            if (t1 && t2 && t3 && t4)
            {
                
               
                
                btnchk.Enabled = true;
                mfrm.msg("该工程可以结束!");
            }
            else
            {
                
                btnchk.Enabled = false;
                mfrm.msg("该工程无法结束!");
            }
        }

        private void btnchkall_Click(object sender, EventArgs e)
        {
            int count = tree.Nodes.Count;
            mfrm.progressBar.Visible = true;
            mfrm.Label.Visible = true;
            mfrm.progressBar.Maximum = count;
            mfrm.progressBar.Value = 0;
            mfrm.progressBar.ProgressType = eProgressItemType.Standard;
            for (int i = 0; i < count; i++)
            {
                string id = tree.Nodes[i].Text;
                if (check_material_finish(id) && check_predict_finish(id) && check_manage_finish(id) && check_work_finish(id))
                {
                    string sql = "update [projects] set [状态]='完成' where [项目编号]='" + id + "'";
                    m_db.ExecuteSQL(sql);

                }
                mfrm.progressBar.PerformStep();
                mfrm.Label.Text = (i + 1).ToString() + "/" + count.ToString();
            }
            mfrm.Label.Visible = false; mfrm.progressBar.Visible = false;
        }

        private void btnchk_Click(object sender, EventArgs e)
        {
            Node n = tree.SelectedNode;
            string id = n.Text;
            message_frm frm = new message_frm("是否要结束该工程核算?", "询问", icon_img.query);
            string time = this.monthCalendarItem1.SelectedDate.ToShortDateString();
            frm.ShowDialog(this);
            if (frm.yesorno == true)
            {
                string sql = string.Format("update [projects] set [状态]='完成' and [结束时间]='{0}' where [项目编号]='{1}'",
                    time, id);
                m_db.ExecuteSQL(sql);
            }
            mfrm.msg("已结束核算");
        }
    }
}
